package com.decoration.basic.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

public class SessionSingleton {

	private static SessionSingleton instance = new SessionSingleton();
	private static Map<String, HttpSession> sessionClientMap = new HashMap<String, HttpSession>();
	private static Map<String, HttpSession> sessionServiceMap = new HashMap<String, HttpSession>();

	private SessionSingleton() {
	}

	public static SessionSingleton getInstance() {
		return instance;
	}

	public void putClientSession(String key, HttpSession val) {
		sessionClientMap.put(key, val);
	}

	public HttpSession getClientSession(String key) {
		return sessionClientMap.get(key);
	}

	public void checkClientSession(String key, HttpSession NewSession) {
		HttpSession session = sessionClientMap.get(key);

		// 重复登陆，会误删session
		if (null != session && NewSession != session) {
			boolean ttt = session.isNew();
			System.out.println("ttt: " + ttt);
			if (ttt) {
				System.out.println("session: " + session);
				System.out.println("NewSession: " + NewSession);
				System.out.println("key: " + key);
				session.invalidate();
				sessionClientMap.remove(key);
			} else {
				sessionClientMap.remove(key);
			}
		}
	}

	public void removeClientSession(String key, HttpSession NewSession) {
		HttpSession session = sessionClientMap.get(key);
		System.out.println("key: " + key);
		// 重复登陆，会误删session
		if (null != session && NewSession == session) {
			System.out.println("session: " + session);
			session.invalidate();
			sessionClientMap.remove(key);
		} else if (null != session) {
			System.out.println("NewSession: " + NewSession);
			NewSession.invalidate();
			sessionClientMap.remove(key);
		} else {
			System.out.println("NewSession: " + NewSession);
			NewSession.invalidate();
		}
	}

	// 后台数据

	public void putServiceSession(String key, HttpSession val) {
		sessionServiceMap.put(key, val);
	}

	public HttpSession getServiceSession(String key) {
		return sessionServiceMap.get(key);
	}

	public void checkServiceSession(String key, HttpSession NewSession) {
		HttpSession session = sessionServiceMap.get(key);

		// 重复登陆，会误删session
		if (null != session && NewSession != session) {
			boolean ttt = session.isNew();
			System.out.println("ttt: " + ttt);
			if (ttt) {
				System.out.println("session: " + session);
				System.out.println("NewSession: " + NewSession);
				System.out.println("key: " + key);
				session.invalidate();
				sessionServiceMap.remove(key);
			} else {
				sessionServiceMap.remove(key);
			}
		}
	}

	public void removeServiceSession(String key, HttpSession NewSession) {
		HttpSession session = sessionServiceMap.get(key);
		System.out.println("key: " + key);
		// 重复登陆，会误删session
		if (null != session && NewSession == session) {
			System.out.println("session: " + session);
			session.invalidate();
			sessionServiceMap.remove(key);
		} else if (null != session) {
			System.out.println("NewSession: " + NewSession);
			NewSession.invalidate();
			sessionServiceMap.remove(key);
		} else {
			System.out.println("NewSession: " + NewSession);
			NewSession.invalidate();
		}
	}

}
